<template>
  <div>
      <p>商品名：<input type="text" v-model="name"></p>
      <p>价格：<input type="text" v-model="price"></p>
      <p>封面：<input type="file" id='img'></p>
      <p>作者：
          <select v-model="cate">
              <option v-for="i in a" :key="i.a" :value="i.id">{{i.name}}</option>
          </select>
      </p>
      <p><input type="button" value="添加" @click="addgood()"></p>
  </div>
</template>

<script>
import axios from 'axios'
export default {
    data(){
        return{
            a:[],
            name:'',
            price:'',
            cate:'1'

        }
    },
    methods:{
        getcatename(){
            axios({
                url:'http://127.0.0.1:8000/app03/goodadd/',
            }).then(res=>{
                this.a = res.data
            })
        },
        addgood(){
            let img = document.getElementById('img').files[0]
            let form_data = new FormData()
            form_data.append('name',this.name)
            form_data.append('price',this.price)
            form_data.append('author',this.cate)
            form_data.append('img',img) 
            axios({
                url:'http://127.0.0.1:8000/app03/goodadd/',
                method:'post',
                data:form_data
            }).then(res=>{
                console.log(res.data.code)
            })
        }

    },
    created(){
        this.getcatename()
    }

}
</script>

<style>

</style>